package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.constants.Common;
import com.itany.exception.UpdateFlagException;
import com.itany.mapper.ExamineMapper;
import com.itany.pojo.Book;
import com.itany.pojo.BookSupplier;
import com.itany.pojo.Examine;
import com.itany.mapper.BookSupplierMapper;
import com.itany.service.ExamineService;
import com.itany.vo.SupplierExamine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author：sl
 * Date：2019-09-18 下午3:30
 * Description：<描述>
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class ExamineServiceImpl implements ExamineService {


    @Autowired
    private ExamineMapper examineMapper;

    @Autowired
    private BookSupplierMapper bookSupplierMapper;

    /**
     * 添加书籍审核
     * 1.添加书籍表，
     * 根据ISBN去查重
     * 将book的数量清空
     * 将book的状态改成未审核，返回主键
     * <p>
     * 2.将book主键加在审核对象中，将审核对象的状态改为未审核，插入审核表
     * <p>
     * <p>
     * 记得添加入库时间
     */
    public void addExamine(Book book, Examine examine) {
        try {
            book.setNumber(0);

            Book rebook = examineMapper.selectBookByISBN(book);
            if (null == rebook) {

                book.setFlag(Common.BookCommons.BOOK_WAIT);
                examineMapper.insertBook(book);//返回主键


                examine.setBookId(book.getId());
                if (null == book.getAuthor()) {
                    book.setAuthor("佚名");
                }
                examine.setTitle(book.getBookName() + "--" + book.getAuthor());
                examine.setFlag(Common.ExamineCommons.EXAMINE_WAIT);

                examineMapper.insertExamine(examine);
            } else {

                examine.setBookId(rebook.getId());
                examine.setFlag(Common.ExamineCommons.EXAMINE_WAIT);
                examine.setTitle(rebook.getBookName() + "--" + rebook.getAuthor());

                examineMapper.insertExamine(examine);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public PageInfo<Examine> findAllExamineByCondition(SupplierExamine supplierExamine, Integer pageNo, Integer pageSize) {

        PageHelper.startPage(pageNo, pageSize);
        List<Examine> examines = examineMapper.selectExamineByCondition(supplierExamine);
        PageInfo<Examine> examinePageInfo = new PageInfo<Examine>(examines);
        return examinePageInfo;
    }

    /**
     * 审核不通过
     * 审核表
     * 将审核表的状态改为disable
     * 将审核信息记录数据库中
     * <p>
     * 书籍表 不用动
     */
    public void disableFlag(Examine examine) throws UpdateFlagException {

        Examine reExamine=examineMapper.selectExamineById(examine.getId());
        if (reExamine.getFlag()!=Common.ExamineCommons.EXAMINE_WAIT ) {
            throw new UpdateFlagException("以审核过,不能重复审核");
        }

        examineMapper.updateExamine(examine);

    }


    /**
     * 审核通过
     * 审核表
     * 查审核是否审核过
     * 如果没审核过，将审核表的状态改为enable
     * 将审核信息记录数据库中
     * 如果审核过，则直接抛异常，以审核过，不能重复审核
     * <p>
     * 书籍表
     * 查询书籍表
     * 判断状态是否为禁用
     * 如果禁用不能修改状态，返回字符串：不能审核通过禁用的书籍
     * 不为禁用则修改状态为通过审核
     * 获取审核对象的书籍的数量，将审核对象中的数量加到书籍总数量中
     * <p>
     * 书籍和供应商的中间表
     * 根据书籍id和供应商id， 查询 书籍和供应商的中间表 获取中间表对象
     * 如果中间表对象为null，将新建的对象插入表中
     * 如果中间表对象不为null，将审核通过的书本数量加到 书籍和供应商的中间表中
     */
    public void enableFlag(Examine examine) throws UpdateFlagException {

        Examine reExamine=examineMapper.selectExamineById(examine.getId());
        if (reExamine.getFlag()!=Common.ExamineCommons.EXAMINE_WAIT ) {
            throw new UpdateFlagException("以审核过,不能重复审核");
        }
        examineMapper.updateExamine(examine);

        Book book = examineMapper.selectBookById(examine.getBookId());
        if (Common.ExamineCommons.EXAMINE_SDISABLE == book.getFlag()) {
            //如果禁用不能修改状态，返回字符串：不能审核通过禁用的书籍
            throw new UpdateFlagException("不能审核通过禁用的书籍");
        } else {
            book.setFlag(Common.BookCommons.BOOK_ENABLE);
            book.setNumber(book.getNumber() + examine.getNumber());

            examineMapper.updateBookFlag(book);
        }

        BookSupplier bookSupplier = new BookSupplier();
        bookSupplier.setBookId(book.getId());
        bookSupplier.setSupplierId(examine.getSupplierId());

        BookSupplier reBS = bookSupplierMapper.selectBookSupplierByBidAndSid(bookSupplier);

        if (null == reBS) {
            bookSupplier.setNumber(examine.getNumber());
            bookSupplierMapper.insert(bookSupplier);
        } else {
            reBS.setNumber(reBS.getNumber() + examine.getNumber());
            bookSupplierMapper.update(reBS);
        }
    }
}
